package com.blog.web.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.common.core.enums.FileEnums;
import com.blog.common.core.exception.ServiceException;
import com.blog.common.core.utils.PageUtils;
import com.blog.common.core.web.page.TableDataInfo;
import com.blog.common.domain.entity.FileInfo;
import com.blog.common.domain.entity.Photo;
import com.blog.common.domain.entity.PhotoAlbum;
import com.blog.common.domain.vo.PhotoVo;
import com.blog.common.domain.enums.ArticleEnums;
import com.blog.common.mapper.dao.FileInfoMapper;
import com.blog.common.mapper.dao.PhotoAlbumMapper;
import com.blog.common.mapper.dao.PhotoMapper;
import com.blog.web.service.PhotoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 照片 服务实现类
 * </p>
 *
 * @author Valerie
 * @since 2023-02-18
 */
@Service
@RequiredArgsConstructor
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {


    private final PhotoAlbumMapper photoAlbumMapper;

    private final PhotoMapper photoMapper;

    private final FileInfoMapper fileInfoMapper;

    @Override
    public PhotoVo listPhotosByAlbumId(Serializable albumId) {
        PhotoAlbum photoAlbum = photoAlbumMapper.selectOne(
                new LambdaQueryWrapper<PhotoAlbum>()
                        .eq(PhotoAlbum::getId, albumId));
        if (ObjUtil.isEmpty(photoAlbum)){
            log.error("相册不存在 ！");
            throw new ServiceException("相册不存在 ！");
        }
        Page<Photo> page = new Page<>(PageUtils.getPageNum(), PageUtils.getPageSize());
        List<String> photoSrcList = photoMapper.selectPage(page,
                new LambdaQueryWrapper<Photo>()
                        .select(Photo::getPhotoSrc)
                        .eq(Photo::getAlbumId, photoAlbum.getId())
                        .orderByDesc(Photo::getId)
        ).getRecords().stream().map(Photo::getPhotoSrc).collect(Collectors.toList());

        return PhotoVo.builder()
                .photoAlbumName(photoAlbum.getAlbumName())
                .photoAlbumCover(photoAlbum.getAlbumCover())
                .photoList(photoSrcList)
                .build();
    }

    @Override
    public TableDataInfo<Photo> listPhotos(Serializable albumId) {
        Page<FileInfo> fileInfoPage = fileInfoMapper.selectPage(PageUtils.getPage(),
                new LambdaQueryWrapper<FileInfo>()
                        .eq(FileInfo::getFileType, FileEnums.SYSTEM_FILE.getCode())
                        .orderByDesc(FileInfo::getCreateTime));
        List<FileInfo> fileInfoList = fileInfoPage.getRecords();
        List<Photo> photoList = fileInfoList.stream().map(item -> Photo.builder()
                .id(IdUtil.getSnowflakeNextId())
                .photoSrc(item.getFilePath())
                .createTime(item.getCreateTime())
                .build()).collect(Collectors.toList());
        return new TableDataInfo<>(photoList, fileInfoPage.getTotal());
    }
}
